"""
自动创建自动适配的PV。
"""
import os
import sys
import shutil


def create_pv(name, count, capacity):
	pv_path = '/mnt/data/'
	parent_path = os.path.join(pv_path, name)
	if os.path.isdir(parent_path):
		shutil.rmtree(parent_path)
	for i in range(int(count)):
		sub_path = os.path.join(parent_path, str(i))
		os.makedirs(sub_path)
		os.system("sed 's/NAME/{}/g' pv.yaml | sed 's/999/{}/g' | sed 's/PATH/{}/g' | kubectl create -f -".format('{}-{}'.format(name, str(i)), capacity, sub_path.replace('/', '\/')))


def main(args):
	pv_info = args
	print(pv_info[0], pv_info[1], pv_info[2])
	create_pv(pv_info[0], pv_info[1], pv_info[2])


if __name__ == '__main__':

	args = sys.argv[1:]
	if not len(args) == 3:
		print('argv number is incorrect. Usages : python3 create_pv.py NAME PV_NUMBER PV_CAPCITY')
		exit(1)
	main(args)
